#'###########################################
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################

rm(list=ls())

source("C:/UQAR/Recherche/Maitrise/Donnees/R/Utils/loadDependencies.R")

STATIC_WORKING_DIR <- "C:/UQAR/Recherche/Maitrise/Donnees/Bylot/Fixe"
MOVEBANK_WORKING_DIR <- "C:/UQAR/Recherche/Maitrise/Donnees/Movebank/Filtered/Statique"
MOVEBANK_STATIC_PATH <- "C:/UQAR/Recherche/Maitrise/Donnees/Movebank/Filtered/Statique/Argos_statique.csv"
MOVEBANK_STATIC_PATH2 <- "C:/UQAR/Recherche/Maitrise/Donnees/Movebank/Filtered/Statique/Argos_statique2.csv"
MOVEBANK_STATIC_PATH3 <- "C:/UQAR/Recherche/Maitrise/Donnees/Movebank/Filtered/Statique/Argos_statique3.csv"


#'#
#'# Recuperation des donnees statiques et comparaison avec le gps
#'#


static.errors.raw <- loadData(STATIC_WORKING_DIR)

static.errors.mb <- loadMovebankData(MOVEBANK_STATIC_PATH)
static.errors.mb2 <- loadMovebankData(MOVEBANK_STATIC_PATH2)
static.errors.mb3 <- loadMovebankData(MOVEBANK_STATIC_PATH3)

#'#################################################################################################
#'#                                                                                              ##
#'#                                 Proportion de localisations                                  ##
#'#                                                                                              ##
#'#################################################################################################

cols <- c("mistyrose2", "indianred3", "darkred")
ylim <- c(0, 85)


static.freqs.raw <- static.freqs(static.errors.raw)
static.freqs.mb <- static.freqs(static.errors.mb)
static.freqs.mb2 <- static.freqs(static.errors.mb2)
static.freqs.mb3 <- static.freqs(static.errors.mb3)

freq.raw <- static.barchart(static.freqs.raw, col=cols, ylimits=ylim,
		main=paste("Donnees brutes, nlocs:", nrow(static.errors.raw)))
freq.mb <- static.barchart(static.freqs.mb, col=cols, ylimits=ylim,
		main=paste("Movebank (LC3, maxredun: 0.5, minrate: 0.5), nlocs:", nrow(static.errors.mb)))
freq.mb2 <- static.barchart(static.freqs.mb2, col=cols, ylimits=ylim,
		main=paste("Movebank (all, maxredun: 0.5, minrate: 0.5), nlocs:", nrow(static.errors.mb2)))
freq.mb3 <- static.barchart(static.freqs.mb3, col=cols, ylimits=ylim,
		main=paste("Movebank (all, maxredun: 0.01, minrate: 0.1), nlocs:", nrow(static.errors.mb3)))

print.multi(list(freq.raw, freq.mb, freq.mb3, freq.mb2), width=500, height=500, save=TRUE,
		save.dest="C:/UQAR/Recherche/Maitrise/Donnees/Resultats/compareStaticFreqs.png")
#print.multi(list(freq.raw, freq.mb), save=TRUE, save.dest="C:/UQAR/Recherche/Maitrise/Donnees/Resultats/compareStaticFreqs.png")

#save.png(freq.raw, "C:/UQAR/Recherche/Maitrise/Donnees/Resultats/staticFreqs.png")
#save.png(freq.mb, "C:/UQAR/Recherche/Maitrise/Donnees/Resultats/staticFreqs_movebank.png")

#'#################################################################################################
#'#                                                                                              ##
#'#                                 Evaluation de l'erreur                                       ##
#'#                                                                                              ##
#'#################################################################################################


#'#
#'# Rayon d'erreur(68e centile) pour chaque visibilite
#'# 

cols <- c("mistyrose2", "indianred3", "darkred")


static.radius.raw <- static.radius(static.errors.raw)
static.radius.mb <- static.radius(static.errors.mb)
static.radius.mb2 <- static.radius(static.errors.mb2)
static.radius.mb3 <- static.radius(static.errors.mb3)

radius.raw <- static.barchart(static.radius.raw, col=cols,
		ylab="Rayon d'erreur(m)",
		main=paste("Donnees brutes, nlocs:", nrow(static.errors.raw)))
radius.mb <- static.barchart(static.radius.mb, col=cols, ylimits=c(0, max(static.radius.raw[, ub], na.rm=TRUE)),
		ylab="Rayon d'erreur(m)",
		main=paste("Movebank (LC3, maxredun: 0.5, minrate: 0.5), nlocs:", nrow(static.errors.mb)))
radius.mb2 <- static.barchart(static.radius.mb2, col=cols, ylimits=c(0, max(static.radius.raw[, ub], na.rm=TRUE)),
		ylab="Rayon d'erreur(m)",
		main=paste("Movebank (all, maxredun: 0.5, minrate: 0.5), nlocs:", nrow(static.errors.mb2)))
radius.mb3 <- static.barchart(static.radius.mb3, col=cols, ylimits=c(0, max(static.radius.raw[, ub], na.rm=TRUE)),
		ylab="Rayon d'erreur(m)",
		main=paste("Movebank (all, maxredun: 0.01, minrate: 0.1), nlocs:", nrow(static.errors.mb3)))

print.multi(list(radius.raw, radius.mb, radius.mb2, radius.mb3),
		width=500, height=500, ncol=2, nrow=2,
		save=TRUE, save.dest="C:/UQAR/Recherche/Maitrise/Donnees/Resultats/compareStaticRadius2.png")
#print.multi(list(radius.raw, radius.mb), save=TRUE, save.dest="C:/UQAR/Recherche/Maitrise/Donnees/Resultats/compareStaticRadius.png")


#'#################################################################################################
#'#                                                                                              ##
#'#                                 Gains et pertes                                       ##
#'#                                                                                              ##
#'#################################################################################################

cols <- c("mistyrose2", "indianred3", "darkred")

res1 <- compare.filters.desc.static(static.errors.raw, static.errors.mb)
res2 <- compare.filters.desc.static(static.errors.raw, static.errors.mb2)
res3 <- compare.filters.desc.static(static.errors.raw, static.errors.mb3)

props <- rbindlist(list(res1[, list(mean=mean(proplocs), sd=sd(proplocs), filter="mb1"), by=LC ],
				res2[, list(mean=mean(proplocs), sd=sd(proplocs), filter="mb2"), by=LC ],
				res3[, list(mean=mean(proplocs), sd=sd(proplocs), filter="mb3"), by=LC ]))


p.static <- barchart(mean ~ LC, props, groups=as.factor(filter), stack=FALSE,
		ly=props[, mean - sd],
		uy=props[, mean + sd], prepanel=prepanel.errorbar, panel=panel.errorbar,
		col=cols, box.ratio=3, origin=0,
		key=list(text=list(c("LC3, maxredun : 0.5, minrate:0.5",
								"All, maxredun: 0.5, minrate: 0.5",
								"All, maxredun: 0.01, minrate: 0.1")), 
				rectangles=list(col=cols), x=1, y=0.98, corner=c(1, 1)),
		xlab="Classes de localisation",
		ylab="Proportion de localisations supprim\u00e9es (%)",
		main="Proportion de localisations supprim\u00e9es en fonction par filtre",
		scales = list(col=1,tck=c(1,0)),
)

print.multi(list(p.static), 
		save=TRUE, save.dest="C:/UQAR/Recherche/Maitrise/Donnees/Resultats/pertesStatic.png")


#'#################################################################################################
#'#                                                                                              ##
#'#                                 Evaluation de l'erreur                                       ##
#'#                                                                                              ##
#'#################################################################################################


#'#
#'# Distributions cumulatives
#'#


cols <- grey(c(.9, .7, .5, .3))

locs.props.raw <- locs.error.prop(static.errors.raw)
locs.props.mb <- locs.error.prop(static.errors.mb)
locs.props.mb2 <- locs.error.prop(static.errors.mb2)
locs.props.mb3 <- locs.error.prop(static.errors.mb3)

g1 <- barchart(locs.props.raw, stack=FALSE, horizontal=FALSE, col=cols, key=list(text=list(colnames(locs.props.raw)), 
				rectangles=list(col=cols), x=0, y=0.98, corner=c(0, 1)),
		ylim=c(0,90),
		xlab="Classes de localisation",
		ylab="Proportion de localisations (%)",
		main=paste("Donnees brutes, nlocs: ", nrow(static.errors.raw)))
g2 <- barchart(locs.props.mb, stack=FALSE, horizontal=FALSE, col=cols, key=list(text=list(colnames(locs.props.raw)), 
				rectangles=list(col=cols), x=0, y=0.98, corner=c(0, 1)),
		ylim=c(0,90),
		xlab="Classes de localisation",
		ylab="Proportion de localisations (%)",
		main=paste("LC3, maxredun: 0.5, minrate: 0.5, nlocs: ", nrow(static.errors.mb)))
g3 <- barchart(locs.props.mb2, stack=FALSE, horizontal=FALSE, col=cols, key=list(text=list(colnames(locs.props.raw)), 
				rectangles=list(col=cols), x=0, y=0.98, corner=c(0, 1)),
		ylim=c(0,90),
		xlab="Classes de localisation",
		ylab="Proportion de localisations (%)",
		main=paste("All, maxredun: 0.5, minrate : 0.5, nlocs: ", nrow(static.errors.mb2)))
g4 <- barchart(locs.props.mb3, stack=FALSE, horizontal=FALSE, col=cols, key=list(text=list(colnames(locs.props.raw)), 
				rectangles=list(col=cols), x=0.4, y=0.98, corner=c(0, 1)),
		ylim=c(0,90),
		xlab="Classes de localisation",
		ylab="Proportion de localisations (%)",
		main=paste("All, maxredun: 0.01, minrate: 0.1, nlocs: ", nrow(static.errors.mb3)))

print.multi(list(g1, g2, g3, g4), width=500, height=500, ncol=2, nrow=2,
		save=TRUE, save.dest="C:/UQAR/Recherche/Maitrise/Donnees/Resultats/compareStaticLocRep.png")

